#include "Animation.h"
#include "../SGD Wrappers/CSGD_TextureManager.h"
#include "../Tiny XML/tinyxml.h"
#include <sstream>
using std::wostringstream;

Animation::Animation(void)
{

	this->m_pTM = nullptr;
	this->m_nImageID = NULL;
	this->m_bLooping = false;
	this->m_bPlaying = false;
	this->m_nCurrFrame = 0;
	this->m_fPlaySpeed =0.0f;
	this->m_fTimeWaited =0.0f;

}


Animation::~Animation(void)
{
	this->m_pTM = nullptr;
	this->m_nImageID = NULL;
	this->m_bLooping = false;
	this->m_bPlaying = false;
	this->m_nCurrFrame = -1;
	this->m_fPlaySpeed =0.0f;
	this->m_fTimeWaited =0.0f;
	this->m_vFrames.clear();
}

void Animation::Initialize(void)
{
	this->m_bLooping = false;
	this->m_bPlaying = false;
	this->m_fPlaySpeed =1.0f;
	this->m_fTimeWaited =1.0f;

	this->m_pTM = CSGD_TextureManager::GetInstance();

	D3DXCOLOR test;
	test.a = 1.0f;
	test.b = 0.5f;
	test.g = 0.5f;
	test.r = 0.5f;

//WORKS!!!
	//this->m_nImageID = CSGD_TextureManager::GetInstance()->LoadTexture(_T("resource/Tokens/testSprite.png"), D3DCOLOR_XRGB(255,100,100) );

	//temps for test
	Frame newframe;
	RECT *newtemp = new RECT();

	//set test animation 1
	//image size 32x 32
	//newtemp->left = 0;
	//newtemp->top = 0;
	//newtemp->right = 40;
	//newtemp->bottom = 60;
	//

	//
	//newframe.m_AnchorPoint.x =0;
	//newframe.m_AnchorPoint.y =30;
	//newframe.m_fFrameDuration =0.1f;
	//newframe.m_RenderRect = *newtemp;

	//m_vFrames.push_back(newframe);

	////set test animation 2
	//newtemp->left = 39;
	//newtemp->top = 0;
	//newtemp->right = 78;
	//newtemp->bottom = 60;
	//
	//newframe.m_AnchorPoint.x =0;
	//newframe.m_AnchorPoint.y =30;
	//newframe.m_fFrameDuration =0.1f;
	//newframe.m_RenderRect = *newtemp;

	//m_vFrames.push_back(newframe);

	////set test animation 3
	//newtemp->left = 81;
	//newtemp->top = 0;
	//newtemp->right = 120;
	//newtemp->bottom = 60;
	//
	//newframe.m_AnchorPoint.x =0;
	//newframe.m_AnchorPoint.y =30;
	//newframe.m_fFrameDuration =0.1f;
	//newframe.m_RenderRect = *newtemp;

	//m_vFrames.push_back(newframe);
	////set test animation 4
	//newtemp->left = 122;
	//newtemp->top = 0;
	//newtemp->right = 163;
	//newtemp->bottom = 60;
	//
	//newframe.m_AnchorPoint.x =0;
	//newframe.m_AnchorPoint.y =30;
	//newframe.m_fFrameDuration =0.1f;
	//newframe.m_RenderRect = *newtemp;

	//m_vFrames.push_back(newframe);
	////set test animation 5
	//newtemp->left = 165;
	//newtemp->top = 0;
	//newtemp->right = 202;
	//newtemp->bottom = 60;
	//
	//newframe.m_AnchorPoint.x =0;
	//newframe.m_AnchorPoint.y =30;
	//newframe.m_fFrameDuration =0.1f;
	//newframe.m_RenderRect = *newtemp;

	//m_vFrames.push_back(newframe);

	////set test animation 6
	//newtemp->left = 205;
	//newtemp->top = 0;
	//newtemp->right = 245;
	//newtemp->bottom = 60;
	//
	//newframe.m_AnchorPoint.x =0;
	//newframe.m_AnchorPoint.y =30;
	//newframe.m_fFrameDuration =0.1f;
	//newframe.m_RenderRect = *newtemp;

	//m_vFrames.push_back(newframe);

	this->LoadAnimationXML();

	
	delete newtemp;
}


void Animation::Update(float fElapsedTime)
{
	if(m_bPlaying == false)
		return;

	this->m_fTimeWaited += fElapsedTime * m_fPlaySpeed;

	if(m_fTimeWaited >= m_vFrames[m_nCurrFrame].m_fFrameDuration)
	{
		this->m_fTimeWaited = 0.0f;
		++m_nCurrFrame;

		if(m_nCurrFrame == m_vFrames.size())
		{
			if(m_bLooping == true)
				m_nCurrFrame =0;
			else
			{
				m_nCurrFrame = m_vFrames.size()-1;
				m_bPlaying = false;
			}
		}
	}
}

void Animation::Render(int nX, int nY, bool bFlipped, float fScale, DWORD dColorWord)
{
	RECT tempFrame = m_vFrames[m_nCurrFrame].m_RenderRect;
	POINT tempAnchorPoint = m_vFrames[m_nCurrFrame].m_AnchorPoint;

	float fScaleX = fScale;

	if(bFlipped == true)
		fScale = -fScaleX;

	//static Image
   //	this->m_pTM->Draw(0,0,0, 7.0f,7.0f);

	
	this->m_pTM->Draw( m_nImageID,	nX - int(tempAnchorPoint.x * fScaleX),
									nY - (int)(tempAnchorPoint.y * fScale), 
									fScaleX, fScale,
									&tempFrame,0.0f,0.0f,0.0f, dColorWord);
}

void Animation::PlayAnimation(float fSpeed, bool bLoop, int Curr)
{
	this->m_fPlaySpeed = fSpeed;
	this->m_bLooping = bLoop;
//	this->m_nCurrFrame = Curr;
//	this->m_nCurrFrame =0;
//	this->m_fTimeWaited = 0.0f;
	this->m_bPlaying = true;
}

void Animation::PauseAnimation(void)
{
	this->m_bPlaying = false;
}

void Animation::ResumeAnimation(void)
{
	this->m_bPlaying = true;
}

bool Animation::LoadAnimationXML(void)
{
//	this->m_vFrames.clear();

	Frame tempF;

	int top, bot, right, left, anchorX, anchorY;

	RECT render;
	RECT passive;
	RECT active;
	double tempDurration;
	bool cool = true;
	std::wostringstream pathLocation;

	// Create a TinyXML Document
	TiXmlDocument animationDoc;


	if(animationDoc.LoadFile("../AnimationEditor/WindowsFormsApplication1/WindowsFormsApplication1/XMLsave/TestAnimation.xml") )
	{
	


	TiXmlElement* pRoot = animationDoc.RootElement();

	if(pRoot != nullptr)
	{

	TiXmlElement* anim = pRoot->FirstChildElement("Frame");
	//<frame> ==#

	//TiXmlElement* framNumebr = pRoot->FirstChildElement("Num of Frame");
	//anim = pRoot->NextSiblingElement("NumOfFrames");
	
	//breaks here
	while (anim != nullptr)
		{
			anim->Attribute("RectTop1", &top);
			anim->Attribute("RectLeft1", &left);
			anim->Attribute("RectRight1", &right);
			anim->Attribute("RectBottom1", &bot);
				render.top = top;
			render.bottom = bot;
			render.right = right;
			render.left = left;

			anim->Attribute("RectTop2", &top);
			anim->Attribute("RectLeft2", &left);
			anim->Attribute("RectRight2", &right);
			anim->Attribute("RectBottom2", &bot);
				passive.top = top;
			passive.bottom = bot;
			passive.right = right;
			passive.left = left;

			anim->Attribute("RectTop3", &top);
			anim->Attribute("RectLeft3", &left);
			anim->Attribute("RectRight3", &right);
			anim->Attribute("RectBottom3", &bot);
			
			anim->Attribute("AnchorX", &anchorX);
			anim->Attribute("AnchorY", &anchorY);

			anim->Attribute("Durration", &tempDurration);

			if(cool == true)
			{
				const char* path= anim->GetText();
				if(path != nullptr)
				{
					pathLocation<<path;

					this->m_nImageID = CSGD_TextureManager::GetInstance()->LoadTexture(pathLocation.str().c_str(), D3DCOLOR_XRGB(255,100,100) );

				}
			}

			cool =false;

			active.top = top;
			active.bottom = bot;
			active.right = right;
			active.left = left;

			tempF.m_ActiveRect = active;
			tempF.m_PassiveRect = passive;
			tempF.m_RenderRect = render;

			tempF.m_AnchorPoint.x = anchorX;
			tempF.m_AnchorPoint.y = anchorY;
			tempF.m_fFrameDuration = (float)tempDurration;

			m_vFrames.push_back(tempF);

			
			//m_XmlFrame = tempF;

			anim = anim->NextSiblingElement("Frame");
		}

	}
	return false;


	}
	else
	{
		return false;
	}

}
